package web.converters;

import java.util.ResourceBundle;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;

import ejb.entity.Position;
import ejb.manager.CityManager;

@ManagedBean
public class PositionConverter implements javax.faces.convert.Converter{
	@EJB
	private CityManager cityManager;
	private ResourceBundle msg;
	
	public PositionConverter(){
		this.msg = ResourceBundle.getBundle("Bundle");
	}
	
	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		String result = null;
		if(!(arg2 instanceof Position)) throw new ConverterException("ERROR converting position!");
		Position position = (Position) arg2;
		result = position.getName();
		
		int temp = (int)cityManager.approxTemp(position.getPositionx(), position.getPositiony());
		int pressure = (int)cityManager.approxPress(position.getPositionx(), position.getPositiony());
		result += " " + temp + msg.getString("celsius");
		result += " " + pressure + " " + msg.getString("hPa");
		
		return result;
	}

}
